package com.zan.zanchat.common.user.service.adapter;

import cn.hutool.core.bean.BeanUtil;
import com.zan.zanchat.common.common.domain.enums.YesOrNoEnum;
import com.zan.zanchat.common.user.domain.entity.ItemConfig;
import com.zan.zanchat.common.user.domain.entity.User;
import com.zan.zanchat.common.user.domain.entity.UserBackpack;
import com.zan.zanchat.common.user.domain.vo.resp.BadgeResp;
import com.zan.zanchat.common.user.domain.vo.resp.UserInfoResp;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author Zan
 * @Create 2024/3/8 16:52
 * @Description :
 */
public class UserAdapter {

    public static User buildUserSave(String openId) {
        return User.builder().openId(openId).build();
    }

    public static User buildAuthorizeUser(Long uid, WxOAuth2UserInfo userInfo) {
        User user = new User();
        user.setId(uid);
        user.setName(userInfo.getNickname());
        user.setAvatar(userInfo.getHeadImgUrl());
        return user;
    }

    public static UserInfoResp buildUserInfo(User user, Integer modifyNameCount) {
        UserInfoResp userInfoResp = new UserInfoResp();
//        BeanUtil.copyProperties(user, userInfoResp);
        userInfoResp.setId(user.getId());
        userInfoResp.setName(user.getName());
        userInfoResp.setAvatar(user.getAvatar());
        userInfoResp.setSex(user.getSex());
        userInfoResp.setModifyNameChance(modifyNameCount);
        return userInfoResp;
    }

    /**
     *
     * @param itemConfigs 所有的徽章列表
     * @param backpacks 用户有哪些徽章
     * @param user
     * @return
     */
    public static List<BadgeResp> buildBadgeResp(List<ItemConfig> itemConfigs, List<UserBackpack> backpacks, User user) {
        Set<Long> obtainItemSet = backpacks.stream().map(UserBackpack::getItemId).collect(Collectors.toSet());

        return itemConfigs.stream().map(itemConfig -> {
            BadgeResp resp = new BadgeResp();
            resp.setId(itemConfig.getId());
            resp.setImg(itemConfig.getImg());
            resp.setDescribe(itemConfig.getDescribe());
            // 是否拥有
            resp.setObtain(obtainItemSet.contains(itemConfig.getId()) ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
            // 是否佩戴
            resp.setWearing(Objects.equals(itemConfig.getId(), user.getItemId()) ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
            return resp;
            // Comparator.reverseOrder()倒序排序
        }).sorted(Comparator.comparing(BadgeResp::getWearing, Comparator.reverseOrder())
                .thenComparing(BadgeResp::getObtain, Comparator.reverseOrder()))
                .collect(Collectors.toList());

    }

    public static UserBackpack buildUserBackpackResp(Long uid, Long itemId, String idempotent) {
        return UserBackpack.builder()
                .uid(uid)
                .itemId(itemId)
                .status(YesOrNoEnum.NO.getStatus()) // 注意这里的发放物品得是NO，即未使用
                .idempotent(idempotent)
                .build();
    }
}
